(function( $ ){

    var methods = {
		init : function(options) {
			options = options == undefined ?{}:options;
			var settings = {};
			settings.mapOptions = $.extend( {
				center:new google.maps.LatLng(37.4419, -100.1419),
				mapTypeId:google.maps.MapTypeId.ROADMAP,
				scrollwheel:false,
				zoom:3
			  }, options.mapOptions);	
			
			settings.pluginOptions = $.extend({
				fitBounds:undefined,
				afterLoad:undefined
			},options.pluginOptions);
			
			window.map = new google.maps.Map(this[0], settings.mapOptions);
			
			for ( var i = 0; options.mapEvents != undefined && i < options.mapEvents.length; i++) {
				var mapEvent = options.mapEvents[i];
				google.maps.event.addListener(window.map,mapEvent.event,mapEvent.action);
			}
			
			window.map.markers = new google.maps.MVCArray();
			if(settings.pluginOptions.fitBounds != undefined){
				window.map.fitBounds(settings.pluginOptions.fitBounds);
			}
			if(settings.pluginOptions.afterLoad!=undefined){
				settings.pluginOptions.afterLoad.call(this);
			}
		},
		
		addMarker: function(options){
			
			var settings = {markerOptions:{}, pluginOptions:{}};
			settings.markerOptions = $.extend({
										map:window.map,
										visible:true
									},options.markerOptions);
			
			
			settings.pluginOptions =  $.extend({
										clearMap:false,
										centerMarker:true
									},options.pluginOptions);
			
			if(settings.pluginOptions.clearMap){
				clear_map_markers();
			}

			var marker = new google.maps.Marker(settings.markerOptions);
			window.map.markers.push(marker);
			
			for ( var i = 0; options.markerEvents != undefined && i < options.markerEvents.length; i++) {
				var markerEvent = options.markerEvents[i];
				google.maps.event.addListener(marker,markerEvent.event,markerEvent.action);
			}
			
			if(settings.pluginOptions.centerMarker){
				window.map.setOptions({center:settings.markerOptions.position,zoom:16});
			}
			
			var bounds = window.map.getBounds();
			if(bounds != undefined){
			
				var sw = bounds.getSouthWest();
				var ne = bounds.getNorthEast();
				var pos = marker.getPosition();
				var minLat = sw.lat() < pos.lat()? sw.lat():pos.lat();
				var minLng = sw.lng() < pos.lng()? sw.lng():pos.lng();
				var maxLat = ne.lat() > pos.lat()? ne.lat():pos.lat();
				var maxLng = ne.lng() > pos.lng()? ne.lng():pos.lng();
				if(sw.lat()!=minLat || sw.lng()!=minLng || ne.lat()!=maxLat || ne.lng()!=maxLng){
					bounds = new google.maps.LatLngBounds( new google.maps.LatLng(minLat,minLng),new google.maps.LatLng(maxLat,maxLng));
					window.map.fitBounds(bounds);
				}
			}
			
		},
		
		addInfoWindow: function(options){
			var settings = {infoWindowOptions:{}, pluginOptions:{}};
			var infoWindow = new google.maps.InfoWindow(options.infoWindowOptions);
			if(window.map.infoWindow != undefined)
				window.map.infoWindow.close();
			window.map.infoWindow = infoWindow; 
			infoWindow.open(window.map,options.pluginOptions.anchor);
		},
		
		clearMarkers: function(options){
			clear_map_markers();
		}, 
		
	};
    
    $.fn.gmap = function(method){
    	// Method calling logic
        if ( methods[method] ) {
          return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on disable' );
        }
    };
})( jQuery );

function clear_map_markers(){
	for(var i=0;i<window.map.markers.getLength();i++){
		window.map.markers.getAt(i).setMap(null);
	}
	
	window.map.markers.clear();
}

function create_lat_lng(lat,lng){
	return new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
}

function create_bound(latSW,lngSW,latNE,lngNE){
	var sw = create_lat_lng(latSW,lngSW);
	var ne = create_lat_lng(latNE,lngNE);
	return new google.maps.LatLngBounds(sw,ne);
}
